<template>
	<div class="bomb">
		<userinfo class="userinfo" :userData="userData"></userinfo>
		<router-link to="/bombpage/add" class="addLink">增加炸弹</router-link>
		<router-view class="addBombView"></router-view>
		<div class="query">
			<div class="title">炸弹列表</div>
			<ul>
				<li v-for="bomb in bombs" :key="bomb.id">{{ bomb.name + '-' + bomb.type }}</li>
			</ul>
		</div>
	</div>
</template>

<script>
import { createNamespacedHelpers } from 'vuex';
import { showTip } from '../../assets/js/tip.js';
import userinfo from './components/UserInfo.vue';
import addbomb from './views/AddBomb.vue';
import axios from 'axios';

const { mapActions, mapState } = createNamespacedHelpers('userlogin');

export default {
	name: 'bomb',
	components: {
		userinfo,
		addbomb,
	},
	data() {
		return {
			bombs: [],
		};
	},
	methods: {
		...mapActions(['isLogin']),
		async getBombs() {
			try {
				const response = await axios.get('/api/bomb/get');
				if (response.data.success) {
					this.bombs = response.data.data;
				}
			} catch (error) {
				showTip('获取失败！' + error.response.data.message);
			}
		},
	},
	computed: {
		...mapState(['userData']),
	},
	async mounted() {
		const islogin = await this.isLogin();
		if (!islogin) {
			// 没有登录就跳转至登录页
			location.href = '/login';
			return;
		}
		showTip('欢迎回来！' + this.userData.nickname);
		this.getBombs();
	},
};
</script>

<style lang="scss" scope>
* {
	margin: 0;
	padding: 0;
	list-style: none;
}

.bomb {
	background-color: rgb(255, 168, 68);
	width: 100%;
	height: 100vh;
	display: flex;
	justify-content: space-evenly;
	align-items: center;

	.userinfo {
		position: absolute;
		top: 16px;
		right: 16px;
	}

	.addLink {
		position: absolute;
		display: block;
		left: 16px;
		top: 16px;
		color: palegreen;
		text-decoration: none;
		font-size: 24px;
		height: 32px;
		line-height: 32px;
		text-align: center;
		width: 125px;
		user-select: none;
		cursor: pointer;

		&:hover {
			color: white;
			background-color: blueviolet;
			border-radius: 3px;
		}
	}

	.query,
	.add {
		height: 50vh;
		width: 35vw;
		border: 3px blue solid;
		border-radius: 8px;
		display: flex;
		flex-direction: column;
		justify-content: flex-start;
		align-items: center;
		text-align: center;

		.title {
			position: relative;
			font-size: 28px;
			top: 8px;
			border-bottom: purple 2px solid;
		}

		ul {
			position: relative;
			border-radius: 5px;
			width: 100%;
			top: 16px;
			height: 80%;

			li {
				height: 36px;
				line-height: 36px;
				font-size: 24px;
				font-family: '华康少女文字W5(P)';
				border-bottom: 1px green dashed;
			}
		}
	}

	.add {
		user-select: none;

		input {
			position: relative;
			height: 36px;
			width: 80%;
			outline: none;
			border-radius: 6px;
			margin-top: 36px;
			top: 64px;
			font-size: 24px;
		}

		.button {
			font-size: 24px;
			position: relative;
			top: 128px;
			width: 80px;
			height: 32px;
			line-height: 32px;
			cursor: pointer;

			&:hover {
				background-color: purple;
				color: white;
				border-radius: 5px;
			}
		}
	}
}
</style>